#ifndef _ITABLEHEADER_H_
#define _ITABLEHEADER_H_

#include <BIcon>
#include <BStyle>
#include <BMargin>
#include <BPainter>
#include <BSizeStretch>

#include "IAlignment.h"

namespace BWE
{
	class IHeaderSection : public IAlignment
	{
	public:
		inline IHeaderSection()
		{
			visible = true;
			sortable = false;
			sortMode = 0;
			ia_align = Align_Center;
			policy = Policy_Preferred;
			stretch = 1.0f;
			dirty = true;
			finished = false;
			dirtyItems = true;
		}

		inline void freshSize(const BStyle* style, const BFont* font)
		{
			if (style)
				style->translate(text, t_text);
			else
				t_text.clear();
			ia_textSize.reset();
			if (font)
			{
				if (t_text.size())
					ia_textSize = font->textSize(t_text);
				else
					ia_textSize = font->textSize(text);
			}
			adjust(BRect());
			hintSize = ia_hintSize;
			if (hintSize.width() < 1)
				hintSize.width() = font->size();
			if (hintSize.height() < 1)
				hintSize.height() = font->size();
			if (sortable)
				hintSize.width() += ia_textSize.height();
			if (hintSize.width() < itemsSize.width())
				hintSize.width() = itemsSize.width();
			if (hintSize.height() < itemsSize.height())
				hintSize.height() = itemsSize.height();
		}

		inline void adjustRect()
		{
			if (sortable)
			{
				BSize arrowSize = rect.height() / 2;
				BRect sortRect = rect;
				sortRect.left() = rect.right() - ia_textSize.height();
				sortUpRect = sortRect.align(Align_TopCenter, arrowSize);
				sortDownRect = sortRect.align(Align_BottomCenter, arrowSize);
				BRect area = rect;
				area.right() = sortRect.left();
				IAlignment::adjust(area);
			}
			else
			{
				IAlignment::adjust(rect);
			}
		}

		bool			visible;
		bool			sortable;
		char			sortMode;
		Policy			policy;
		float			stretch;
		BString			text;
		BValue			asset;

		bool			dirty;
		bool			finished;
		bool			dirtyItems;
		BSize			itemsSize;
		BSize			realSize;
		BSize			hintSize;
		BString			t_text;
		BRect			rect;
		BRect			sortUpRect;
		BRect			sortDownRect;
	};

	class IHeader
	{
	public:
		IHeader();
		~IHeader();

		void correctHandle();
		BRect handleRect(int index);
		BRange xrange(int minx, int maxx);
		BRange yrange(int miny, int maxy);

		int sectionUnderPoint(const BPoint& point);
		int handleUnderPoint(const BPoint& point);
		void distributeWidth(int width);
		void distributeHeight(int height);

		bool dragBegin(const BPoint& pos);
		bool drag(const BPoint& pos);
		void paint(BPainter& painter);

		Orientation		orientation;
		int				spacing;
		bool			visible;
		BIconHolder		upIcon;
		BIconHolder		downIcon;

		bool			resizable;
		int				hoverIndex;
		bool			pressed;
		BPoint			pressedPoint;
		BSize			pressedSize;
		int				handle;
		bool			draging;
		BSize			hintSize;
		BSize			realSize;
		int				show_begin;
		int				show_end;
		BArray<IHeaderSection>	sections;
	};

}

#endif